package com.boyinet.imesh2json.fx.controller

import com.boyinet.imesh2json.viewmodel.DataMapsViewModel
import de.felixroske.jfxsupport.FXMLController
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.TextField
import javafx.scene.layout.AnchorPane
import jfxtras.styles.jmetro.JMetro
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL
import java.util.*

@FXMLController
class MapDialogController : Initializable {

    @FXML
    lateinit var root: AnchorPane

    @Autowired
    lateinit var dataMapsViewModel: DataMapsViewModel

    @FXML
    lateinit var node: TextField

    @FXML
    lateinit var part: TextField

    @FXML
    lateinit var index: TextField

    @FXML
    lateinit var field: TextField


    override fun initialize(location: URL?, resources: ResourceBundle?) {
        val jMetro = JMetro()
        jMetro.parent = root
        node.textProperty().bindBidirectional(dataMapsViewModel.current.node)
        part.textProperty().bindBidirectional(dataMapsViewModel.current.part)
        index.textProperty().bindBidirectional(dataMapsViewModel.current.index)
        field.textProperty().bindBidirectional(dataMapsViewModel.current.field)

    }

    fun save() {
        if (dataMapsViewModel.update) {
            dataMapsViewModel.update()
        } else {
            dataMapsViewModel.save()
        }
        root.scene.window.hide()
    }

    fun cancel() {
        dataMapsViewModel.current.clear(dataMapsViewModel.currentTemplateId!!)
        root.scene.window.hide()
    }
}
